package cn.gp.chatclient.config;

import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.net.Socket;

@Slf4j
@Data
public class ClientConfig {
    private static final ClientConfig INSTANCE = new ClientConfig();
    
    // 服务器配置
//    private static final String SERVER_HOST = "106.12.212.167";
//    private static final int HTTP_PORT = 9001;
//    private static final int WS_PORT = 9001;

    // 本地配置
    private static final String SERVER_HOST = "127.0.0.1";
    private static final int HTTP_PORT = 9001;
    private static final int WS_PORT = 9001;
    
    // HTTP 请求地址
    public static final String HTTP_BASE_URL = String.format("http://%s:%d/", SERVER_HOST, HTTP_PORT);
    
    // WebSocket 请求地址
    public static final String WS_BASE_URL = String.format("ws://%s:%d/ws/chat/", SERVER_HOST, WS_PORT);
    
    // 聊天历史 API 基础路径
    public static final String CHAT_HISTORY_BASE_URL = String.format("http://%s:%d/api/messages/history", SERVER_HOST, WS_PORT);
    
    private ClientConfig() {
        // 私有构造函数
    }
    
    public static ClientConfig getInstance() {
        return INSTANCE;
    }
    
    /**
     * 获取WebSocket聊天地址
     * @param userId 用户ID
     * @return WebSocket URL
     */
    public static String getChatWebSocketUrl(Long userId) {
        return WS_BASE_URL + userId;
    }
    
    /**
     * 检查服务器连接状态
     * @return 连接是否成功
     */
    public static boolean checkServerConnection() {
        try {
            // 检查 HTTP 服务器
            try (Socket socket = new Socket(SERVER_HOST, HTTP_PORT)) {
                log.info("HTTP服务器连接成功");
            }
            // 检查 WebSocket 服务器
            try (Socket socket = new Socket(SERVER_HOST, WS_PORT)) {
                log.info("WebSocket服务器连接成功");
            }
            return true;
        } catch (Exception e) {
            log.error("服务器连接失败", e);
            return false;
        }
    }
} 